package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.Contract;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public List<NursingVo> findAll() {
        List<NursingVo> nursingVoList = nursingMapper.selectAll();
        return nursingVoList;
    }

    @Override
    public void add(NursingPlanDto dto) {
        if (nursingMapper.selectByName(dto.getPlanName()) != null){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXIST);
        }
        nursingMapper.insert(dto);
        for (NursingProjectPlanDto nursingProjectPlan : dto.getNursingProjectPlans()) {
            nursingProjectPlan.setPlanId(dto.getId());
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        }
    }

    @Override
    public PageResponse<NursingVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<Contract>> page = nursingMapper.selectByPage(name,status);
        return PageResponse.of(page, NursingVo.class);
    }
}
